import json
import urllib.request

import jsonpath

# 1. 获取网页数据(后端发送回来的json)保存在本地
url = 'https://dianying.taobao.com/cityAction.json?activityId&_ksTS=1718421749739_108&jsoncallback=jsonp109&action=cityAction&n_s=new&event_submit_doGetAllRegion=true'
headers = {
    # ': '请求头直接丢弃
    # ':authority': 'dianying.taobao.com',
    # ':method': 'GET',
    # ':path': '/cityAction.json?city=310100&_ksTS=1718420990803_19&jsoncallback=jsonp20&action=cityAction&n_s=new&event_submit_doLocate=true',
    # ':scheme': 'https',
    'accept': 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
    # 'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'zh-CN,zh;q=0.9',
    'bx-v': '2.5.11',
    'cookie': 'cna=meygG5SDcFkCAT2JrdTHB6N4; miid=64020641274688623; thw=cn; tfstk=eXDHU6Az0XPBcPBjX2eBBWhPZdRTRww7z4B8y8Uy_PzsvTKIvfqryVtWAJnzr40T5DBJADhGqmgir7tQyzzoy0lpXKpxADw7UTYvHKpc0LLJkRE4XKoQF88OahoUnDgv8LnVD1BhpbjSUQF4oiqfkgSRz_zjYPXmfAqFgrvbSTyiQkVnUmzgtWDgxW4V4i1NggBcF5z1Z_17TlZgkTJbzNEy-8uwjhf4NWr_AdxMj_17TlZgkhxGg4NUfk9G.; t=308354ec64293ce30965ec58823b3205; cookie2=11ca39f9c00455d592cc2f7803b8e861; v=0; _tb_token_=5bb3a4a53377e; xlly_s=1; tb_city=310100; tb_cityName="yc+6ow=="; isg=BHd3G7Bt3SMQvlylxo0O1a_eBmvBPEueOJ8FDMkkk8ateJe60Qzb7jVeWtgmkCMW',
    'referer': 'https://dianying.taobao.com/?spm=a1z21.3046609.city.2.4398112aMNGCrg&city=310100',
    'sec-ch-ua': '"Chromium";v="9", "Not?A_Brand";v="8"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-origin',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 SLBrowser/9.0.3.5211 SLBChan/105',
    'x-requested-with': 'XMLHttpRequest',
}
my_request = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(my_request)
json_data = response.read().decode('utf-8')
json_data = json_data[13:-2]

with open("jsonpath02-基本使用.json", "w", encoding="utf-8") as f:
    f.write(json_data)

dict1 = json.loads(json_data)  # json转字典
print(type(dict1))

print(jsonpath.jsonpath(dict1, "$..regionName"))
